﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace urBook.Engine
{
    /// <summary>
    /// Class ContactCard represents a person object in the Address Book
    /// </summary>
    public class ContactCard : IComparable, ICloneable
    {
        [XmlIgnore]
        static int idProvider = 1;
        
        public ContactCard()
        {
            id = idProvider++;
            name = new Name();
            photo = new Photo();
            company = "";
            title = "";
            phones = new List<Telephone>();
            homeAddress = new Address() { type = Type.HOME };
            workAddress = new Address() { type = Type.WORK };
            emails = new List<Email>();
            url = "http://";
            notes = "";
            groups = new List<string>();
        }
        [XmlIgnore]
        public int id { get; set; }
        [XmlElement("Name")]
        public Name name { get; set; }
        [XmlElement("Photo")]
        public Photo photo { get; set; }

        [XmlAttribute("company")]
        public string company { get; set; }
        [XmlAttribute("title")]
        public string title { get; set; }

        [XmlArray("Telephones")]
        [XmlArrayItem("Telephone")]
        public List<Telephone> phones { get; set; }

        [XmlElement("HomeAddress")]
        public Address homeAddress { get; set; }
        [XmlElement("WorkAddress")]
        public Address workAddress { get; set; }

        [XmlArray("Emails")]
        [XmlArrayItem("Email")]
        public List<Email> emails { get; set; }

        [XmlElement("Url")]
        public string url { get; set; }
        [XmlElement("Notes")]
        public string notes { get; set; }
        [XmlArray("Groups")]
        [XmlArrayItem("Group")]
        public List<string> groups { get; set; }

        public override string ToString()
        {
            string result = "";

            result += name.ToString() + "\n";

            if (title != "")
                result += title + " ";
            if (company != "")
                result += company + "\n";

            foreach (Telephone t in phones)
                result += "Phone " + t.ToString() + "\n";
            foreach (Email e in emails)
                result += "Email " + e.ToString() + "\n";

            if (homeAddress != null)
                result += "Address " + homeAddress.ToString() + "\n";
            if (workAddress != null)
                result += "Address " + workAddress.ToString() + "\n";

            if (url != "")
                result += "URL: " + url + "\n";
            if (notes != "")
                result += "Notes: " + notes;

            return result;
        }

        public string toVcfString()
        {
            string result = "";
            result += "BEGIN:VCARD" + "\n" +
                      "VERSION:2.1" + "\n";
            if (name != null)
                result += name.toVcfString();
            if (company != null)
                result += "ORG:" + company + "\n";
            if (title != null)
                result += "TITLE:" + title + "\n";
            if (photo != null)
                result += photo.toVcfString() + "\n";
            foreach (Telephone phone in phones)
                result += phone.toVcfString();
            foreach (Email email in emails)
                result += email.toVcfString();
            if (homeAddress != null)
                result += homeAddress.toVcfString();
            if (workAddress != null)
                result += workAddress.toVcfString();
            if (url != null)
                result += "URL:" + url + "\n";
            if (groups.Count > 0)
            {
                result += "X-CATEGORIES:";
                for (int i = 0; i < groups.Count - 1; i++)
                    result += groups[i] + ";";
                result += groups.Last() + "\n";
            }
            if (notes != null)
                result += notes;
            result += "END:VCARD" + "\n";
            return result;
        }
        /*
        public string toXmlString()
        {
            string result = "<Contact" +
                ((title != null) ? (" title=\"" + title + "\"") : "") +
                ((company != null) ? (" company=\"" + company + "\"") : "") + ">";

            result += name.toXmlString();

            if (photo != null)
                result += photo.toXmlString();

            foreach (Telephone phone in phones)
                result += phone.toXmlString();

            foreach (Email email in emails)
                result += email.toXmlString();

            if (homeAddress != null)
                result += homeAddress.toXmlString();

            if (workAddress != null)
                result += workAddress.toXmlString();

            if (notes != null)
                result += "<Notes>" + notes + "</Notes>";

            result += "</Contact>";
            return result;
        }
        */
        public int CompareTo(object obj)
        {
            ContactCard c = (ContactCard)obj;
            return this.name.formattedName.ToLower().Trim().CompareTo(c.name.formattedName.ToLower().Trim());
        }

        public object Clone()
        {
            ContactCard clone = new ContactCard()
            {
                id = this.id,
                company = this.company,
                title = this.title,
                url = this.url,
                notes = this.notes
            };

            clone.name = (Name)this.name.Clone();
            clone.photo = (Photo)this.photo.Clone();
            clone.homeAddress = (Address)this.homeAddress.Clone();
            clone.workAddress = (Address)this.workAddress.Clone();
            
            foreach (Telephone t in this.phones)
                clone.phones.Add((Telephone)t.Clone());
            foreach (Email e in this.emails)
                clone.emails.Add((Email)e.Clone());
            foreach (string g in this.groups)
                clone.groups.Add(g);

            return clone;
        }
    }
}
